import { ref, reactive, onMounted, } from "vue";
import { type PaginationProps } from "@pureadmin/table";
import { useRoute } from "vue-router";
import qs from "qs";
import { ElMessage } from "element-plus";
import { emitter } from "@/utils/mitt";
import Sortable from 'sortablejs'
import { vLoading } from "element-plus";
import {
  GetDictionaryByDictCode,
  AlgorithmicCreditGetAllByPagedAndSort,
} from "@/api/stu/SupplyChainFinance";
import { nextTick } from "process";
export function usePortrait() {
  const dataList = ref();
  const detailData = ref()
  const algorithmList = ref() //左边算法
  const paramsList = ref()//右边参数
  const activeTab = ref()
  const route = useRoute()
  const loading = ref<boolean>(false);
  const tLoding = ref<boolean>(false)
  const pagination = reactive<PaginationProps>({
    total: 0,
    pageSize: 10,
    currentPage: 1,
    pageSizes: [5, 10, 15],
    background: true
  });
  const pagination2 = reactive<PaginationProps>({
    total: 0,
    pageSize: 10,
    currentPage: 1,
    pageSizes: [5, 10, 15],
    background: true
  });

  const columns: TableColumnList = [
    {
      label: "序号",
      type: "index",
      minWidth: 100
    }, {
      label: "模型名称",
      prop: "algorithmModelName"
    }, {
      label: "算法类型",
      prop: "type",
      cellRenderer: ({ row }) => (
        <el-tag type={
          row.type == 4 ? 'warning' : row.type == 5 ? 'success' : row.type == 6 ? 'error' : 'info'
        }>
          {
            row.type == 4 ? "XGBoost回归" : row.type == 5 ? '决策树回归' : row.type == 6 ? 'K近邻（KNN）回归' : '随机森林回归'
          }
        </el-tag>
      )
    }, {
      label: "变量选择",
      prop: "indexSelectionName"
    },
    {
      label: "训练数据",
      prop: "trainData"
    },
    {
      label: "详情",
      prop: "operation",
      width: 180,
      slot: "operation"
    },
  ];
  const handleCurrentChange = val => {
    getList()
  };
  function handleSizeChange() {
    getList()
  }

  //获取任务列表
  async function getList() {
    const data = {
      taskId: route.query.taskId,
      planId: route.query.planId,
      pageIndex: pagination.currentPage,
      pageCount: pagination.pageSize,
      modelType: 2,
    };
    const respone = await AlgorithmicCreditGetAllByPagedAndSort(data);
    if (respone.success) {
      respone?.data.items.forEach(item => item.trainData = '电商平台供应商数据')
      dataList.value = respone?.data.items
      pagination.total = respone.data.totalCount;
    } else {
      ElMessage.error(respone.msg)
    }
  }

  function handleCancelShowChart() {
    tLoding.value = false
  }
  onMounted(async () => {
    getList()
    let data = qs.stringify({
      dictCode: 'QuotaAssessment'
    })
    const res = await GetDictionaryByDictCode(data)
    if (res.success) {
      paramsList.value = res.data
    } else {
      ElMessage.error(res.msg)
    }
    let data1 = qs.stringify({
      dictCode: 'QuotaAssessmentAlgorithm'
    })
    const res1 = await GetDictionaryByDictCode(data1)
    if (res1.success) {
      algorithmList.value = res1.data
      activeTab.value = algorithmList.value[0].dictName
    } else {
      ElMessage.error(res1.msg)
    }
  })
  function handleClick({ props }) {
    activeTab.value = props.name
  }

  async function checkPor(row) {
    console.log(row.type);
    if (row.type == 4) {
      activeTab.value = 'XGBoost回归'
    }
    else if (row.type == 5) {
      activeTab.value = '决策树回归'
    }
    else if (row.type == 6) {
      activeTab.value = 'K近邻（KNN）回归'
    } else {
      activeTab.value = '随机森林回归'
    }
    console.log(activeTab.value);

    detailData.value = JSON.parse(row.algorithmCalculateResult)
    tLoding.value = true
  }
  emitter.on("closeNewApprove", () => {
    activeTab.value = algorithmList.value[0].dictName
    tLoding.value = false
    detailData.value = ''
  })

  emitter.on("refreshTableApprove", () => {
    getList()
  })
  return {
    columns,
    loading,
    dataList,
    pagination,
    tLoding,
    handleSizeChange,
    handleCurrentChange,
    handleCancelShowChart,
    algorithmList,
    paramsList,
    activeTab,
    handleClick,
    checkPor,
    detailData,
  }
}